"""
进度 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz import process_biz
from components.audit_log import audit
from components.depends import login_required
from components.global_param import get_user_id
from components.logger import log
from components.response import OKWithDataResponse
from models.request import (
    ProcessDetailRequest,
    ProcessListRequest,
    ProcessPathField,
    ProcessTerminateRequest,
)
from models.response import (
    ProcessDetailResponse,
    ProcessListResponse,
)
from models.vo import (
    ProcessDetailVO,
    ProcessListPage,
)
from utils.data_utils.special import ObjectIdStr

process_router: APIRouter = APIRouter(prefix="/process")
""" 进度 路由 """


@process_router.get(
    path="/",
    summary="进度",
    response_model=ProcessListResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.PROCESS,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.process_id",
)
async def process_list_mine(request: ProcessListRequest = Depends(ProcessListRequest.from_query)) -> OKWithDataResponse:
    vo: ProcessListPage = process_biz.ProcessGetter.list(request)
    log.info(f"[进度][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)


@process_router.get(
    path="/{process_id}/",
    summary="进度 | 详情",
    response_model=ProcessDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.PROCESS,
    operation=enum.ModelOperation.DETAIL,
    po_id="process_id",
)
async def process_detail(process_id: ObjectIdStr = ProcessPathField.process_id) -> OKWithDataResponse:
    request = ProcessDetailRequest(process_id=process_id)
    vo: ProcessDetailVO = process_biz.ProcessGetter.detail(request)
    log.info(f"[进度][详情] user_id: {get_user_id()} process_id: {process_id}")

    return OKWithDataResponse(vo)


@process_router.put(
    path="/{process_id}/terminate/",
    summary="进度 | 终止",
    response_model=ProcessDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.PROCESS,
    operation=enum.ModelOperation.TERMINATE,
    po_id="process_id",
)
async def process_terminate(process_id: ObjectIdStr = ProcessPathField.process_id) -> OKWithDataResponse:
    request = ProcessTerminateRequest(process_id=process_id)
    vo: ProcessDetailVO = process_biz.ProcessTerminateHandler.terminate(request)
    log.info(f"[进度][终止] user_id: {get_user_id()} process_id: {process_id}")

    return OKWithDataResponse(vo)
